﻿using Yoozoo.Mars.Got.SpriteAnim;
using UnityEngine;

namespace Yoozoo.Modules.InnerCity
{
    public class CityAISpriteTemplate : Interface_AnimSpriteSupportTemplate
    {
        private static float DEGREE_THRESHOLD = 22.5f;//DataManager.Instance.worldDataMgr.CfgData.KVCfgMgr.GetFloatValue("INTERNAL_ANGLE", 45f);//Jiaqi fix the code
        public static float COS_THRESHOLD = Mathf.Cos(DEGREE_THRESHOLD * Mathf.Deg2Rad);

        private static Vector3 Vector_RightUp = new Vector3(0, 0, 1);

        public DIRCTION_INDEX_ENUM GetDirectionIndex(Vector3 dir)
        {
            var angle = Vector3.Angle(Vector_RightUp, dir);
            if (dir.x < 0)
                angle = 360 - angle;

            DIRCTION_INDEX_ENUM dirIdx = DIRCTION_INDEX_ENUM.RIGHT_UP;
            if (angle >= 360 - DEGREE_THRESHOLD || angle < DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_UP;
            else if (angle >= DEGREE_THRESHOLD && angle < 45 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT;
            else if (angle >= 45 + DEGREE_THRESHOLD && angle < 90 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_DOWN;
            else if (angle >= 90 + DEGREE_THRESHOLD && angle < 135 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.DOWN;
            else if (angle >= 135 + DEGREE_THRESHOLD && angle < 180 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_DOWN;
            else if (angle >= 180 + DEGREE_THRESHOLD && angle < 225 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.LEFT;
            else if (angle >= 225 + DEGREE_THRESHOLD && angle < 270 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_UP;
            else if (angle >= 270 + DEGREE_THRESHOLD && angle < 315 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.UP;
            
            return dirIdx;
        }
        
        public DIRCTION_INDEX_ENUM GetDirectionIndex16Direct(Vector3 dir)
        {
            var angle = Vector3.Angle(Vector_RightUp, dir);
            if (dir.x < 0)
                angle = 360 - angle;

            DIRCTION_INDEX_ENUM dirIdx = DIRCTION_INDEX_ENUM.RIGHT_UP;
            if (angle >= 360 - DEGREE_THRESHOLD || angle < DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_UP;
            else if (angle >= DEGREE_THRESHOLD && angle < 45 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT;
            else if (angle >= 45 + DEGREE_THRESHOLD && angle < 90 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_DOWN;
            else if (angle >= 90 + DEGREE_THRESHOLD && angle < 135 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.DOWN;
            else if (angle >= 135 + DEGREE_THRESHOLD && angle < 180 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_DOWN;
            else if (angle >= 180 + DEGREE_THRESHOLD && angle < 225 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.LEFT;
            else if (angle >= 225 + DEGREE_THRESHOLD && angle < 270 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_UP;
            else if (angle >= 270 + DEGREE_THRESHOLD && angle < 315 + DEGREE_THRESHOLD)
                dirIdx = DIRCTION_INDEX_ENUM.UP;
            
            return dirIdx;
        }

        public Material GetMaterial(string altasName = "")
        {
            return null;// CityDisplayController.GetInstance().SpriteMaterial;
        }

        public Vector3 GetBillBoardRotation()
        {
            return new Vector3(320,0,0);
        }
    }
}